package net.fdukedom.epicurus.domain.util;

import java.util.List;

import net.fdukedom.epicurus.domain.entity.User;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.LogicalExpression;

/**
 * Utility class for Users' routine.
 *
 * @author Dmitry Y. Kochelaev
 */
public class UserHelper {

    /**
     * The only instance of the class.
     */
    private static UserHelper instance = new UserHelper();

    /**
     * Default constructor.
     */
    private UserHelper() {
        //this is an utility class only one instance is allowed
    }

    /**
     * Returns the only instance of the class.
     *
     * @return the only instance of the class
     */
    public static UserHelper getInstance() {
        return instance;
    }

    /**
     * Saves user record in database.
     *
     * @param user user to save
     */
    public synchronized void saveUser(User user) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.saveOrUpdate(user);
        session.getTransaction().commit();
    }

    /**
     * Loads user by id.
     *
     * @param id user id
     * @return user with specified id or null if no user was found
     */
    public User load(int id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(User.class);
        List users = criteria.add(Expression.eq("id", id)).list();
        if (users.size() > 0) {
            return (User) users.get(0);
        }
        return null;
    }

    /**
     * Finds user in database by e-mail or name
     *
     * @param email user e-mail
     * @param name user name
     * @return if user with such name OR e-mail was found
     */
    public boolean findByEmailOrName(String email, String name) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(User.class);
        LogicalExpression expression = Expression.or(Expression.like("email", email),
                                                     Expression.like("name", name));

        List users = criteria.add(expression).list();
        return (users.size() > 0);
    }

    /**
     * Finds user in database by e-mail.
     *
     * @param email user e-mail
     * @return user with corresponding email or null if no user was found
     */
    public User findByEmail(String email) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(User.class);
        List users = criteria.add(Expression.like("email", email)).list();
        if (users.size() > 0) {
            return (User) users.get(0);
        }
        return null;
    }

    /**
     * Finds user in database by name.
     *
     * @return user with corresponding name or null if no user was found
     */
    public User findByName(String name) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(User.class);
        List users = criteria.add(Expression.like("name", name)).list();
        if (users.size() > 0) {
            return (User) users.get(0);
        }
        return null;
    }
}
